//
//  FacilityDisplayMain.m
//  YOGProj
//
//  Created by liyun on 14-6-26.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "HCFacilityDisplayMain.h"
#import "HCMacroDefinition.h"
#import "ShowVenuesViewController.h"
#import "HCCustomSelect.h"
#import "UIViewController+MMDrawerController.h"

@interface HCFacilityDisplayMain ()<UITableViewDataSource,UITableViewDelegate,CustomSelectProtocol>
{
    IBOutlet UITableView *facilityDisplayTable;
    NSArray *titleArr;
    NSArray *imageNameArr;
    NSDictionary *faclityCodeDic;
    NSArray          *tempSelectArr;
    HCCustomSelect     *custom;
    NSDictionary *tempSelectDic;
}

@property (nonatomic, strong)UIViewController *leftCtrl;
@property (nonatomic, strong)UIViewController *rightCtrl;


@end

@implementation HCFacilityDisplayMain
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)navigationShowLeft
{
    NSLog(@"左边栏");
    [self.navigationController popViewControllerAnimated:YES];
}

//
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.mm_drawerController.leftDrawerViewController = self.leftCtrl;
    self.mm_drawerController.rightDrawerViewController = self.rightCtrl;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //
    self.leftCtrl = self.mm_drawerController.leftDrawerViewController;
    self.rightCtrl = self.mm_drawerController.rightDrawerViewController;
    
    self.title = NSLocalizedString(@"HC3D场馆", @"");
//    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame=CGRectMake(0, 0, 30, 30);
//    [button setBackgroundImage:[UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HCmain_menu" ofType:@"png"]] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(navigationShowLeft) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *barItemleft=[[UIBarButtonItem alloc]initWithCustomView:button];
//    self.navigationItem.leftBarButtonItem=barItemleft;
    
    //UITableView
    int height                          = (MODEL?568:480)-64;
    facilityDisplayTable                = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, height)
                                                               style:UITableViewStylePlain];
    facilityDisplayTable.delegate       = self;
    facilityDisplayTable.dataSource     = self;
    facilityDisplayTable.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view                          addSubview:facilityDisplayTable];
    
    NSArray *languages = [NSLocale preferredLanguages];
    NSString *currentLanguage = [languages objectAtIndex:0];
    
    if([currentLanguage isEqualToString:@"zh-Hans"])
    {
        faclityCodeDic = @{@"南京青奥村":@{@"南京青奥村":@"AYV2"},
                           @"南京国际博览中心":@{@"南京国际博览中心D馆":@"IED",@"南京国际博览中心E馆":@"IEE",@"南京国际博览中心F馆":@"IEF"},
                           @"南京奥体中心":@{@"南京奥体中心游泳馆":@"OSN",@"南京奥体中心体育场":@"OSS",@"南京奥体中心体育馆":@"OSG"},
                           @"南京水上运动学校":@{@"南京水上运动学校":@"XRC"},
                           @"玄武湖铁人三项赛场":@{@"玄武湖铁人三项赛场":@"XWT"},
                           @"五台山":@{@"五台山篮球场":@"WTB",@"五台山体育馆":@"WTG",@"五台山体育场":@"WTS"},
                           @"江宁体育中心":@{@"江宁体育中心体育馆":@"JNG",@"江宁体育中心体育场":@"JNS"},
                           @"青奥体育公园":@{@"青奥体育公园沙滩排球赛场":@"SPB",@"青奥体育公园曲棍球场":@"SPH",@"青奥体育公园橄榄球场":@"SPR"},
                           @"龙江体育馆":@{@"龙江体育馆":@"LJG"}};
        titleArr = [[NSArray alloc] initWithObjects:@"南京青奥村",
                    @"南京国际博览中心",
                    @"南京奥体中心",
                    @"南京水上运动学校",
                    @"玄武湖铁人三项赛场",
                    @"五台山",
                    @"江宁体育中心",
                    @"青奥体育公园",
                    @"龙江体育馆",
                    nil];
    }
    else if([currentLanguage isEqualToString:@"en"])
    {
        faclityCodeDic = @{@"Youth Olympic Village":@{@"Youth Olympic Village":@"AYV2"},
                           @"Nanjing International Expo Centre":@{@"Nanjing International Expo Centre Hall D":@"IED",@"Nanjing International Expo Centre Hall E":@"IEE",@"Nanjing International Expo Centre Hall F":@"IEF"},
                           @"Nanjing Olympic Sports Centre":@{@"Nanjing Olympic Sports Centre Natatorium":@"OSN",@"Nanjing Olympic Sports Centre Stadium":@"OSS",@"Nanjing Olympic Sports Centre Gymnasium":@"OSG"},
                           @"Nanjing Rowing-Canoeing School":@{@"Nanjing Rowing-Canoeing School":@"XRC"},
                           @"Xuanwu Lake Triathlon Venue":@{@"Xuanwu Lake Triathlon Venue":@"XWT"},
                           @"Wutaishan":@{@"Wutaishan Basketball Venue":@"WTB",@"Wutaishan Gymnasium":@"WTG",@"Wutaishan Stadium":@"WTS"},
                           @"Jiangning Sports Centre":@{@"Jiangning Sports Centre Gymnasium":@"JNG",@"Jiangning Sports Centre Stadium":@"JNS"},
                           @"Youth Olympic Sports Park":@{@"Nanjing Youth Olympic Sports Park Beach Volleyball Venue":@"SPB",@"Nanjing Youth Olympic Sports Park Hockey Field":@"SPH",@"Nanjing Youth Olympic Sports Park Rugby Field":@"SPR"},
                           @"Longjiang Gymnasium":@{@"Longjiang Gymnasium":@"LJG"}};
        titleArr = [[NSArray alloc] initWithObjects:@"Youth Olympic Village",
                    @"Nanjing International Expo Centre",
                    @"Nanjing Olympic Sports Centre",
                    @"Nanjing Rowing-Canoeing School",
                    @"Xuanwu Lake Triathlon Venue",
                    @"Wutaishan",
                    @"Jiangning Sports Centre",
                    @"Youth Olympic Sports Park",
                    @"Longjiang Gymnasium",
                    nil];
    }
    else
    {
        faclityCodeDic = @{@"VOJ":@{@"VOJ":@"AYV2"},
                           @"Centre d’exposition international de Nanjing":@{@"Centre d’exposition international de Nanjing Hall D":@"IED",@"Centre d’exposition international de Nanjing Hall E":@"IEE",@"Centre d’exposition international de Nanjing Hall F":@"IEF"},
                           @"Centre olympique de Nanjing":@{@"Centre olympique de Nanjing Piscine":@"OSN",@"Centre olympique de Nanjing Stade":@"OSS",@"Centre olympique de Nanjing Gymnase":@"OSG"},
                           @"Ecole des sports nautiques de Nanjing":@{@"Ecole des sports nautiques de Nanjing":@"XRC"},
                           @"Site de triathlon du lac Xuanwu":@{@"Site de triathlon du lac Xuanwu":@"XWT"},
                           @"Site de Wutaishan":@{@"Site de Wutaishan Site de basket-ball":@"WTB",@"Site de Wutaishan Gymnase":@"WTG",@"Site de Wutaishan Stade":@"WTS"},
                           @"Centre sportif de Jiangning":@{@"Centre sportif de Jiangning Gymnase":@"JNG",@"Centre sportif de Jiangning Stade":@"JNS"},
                           @"Parc des sports des Jeux Olympiques de la Jeunesse":@{@"Parc des sports des Jeux Olympiques de la Jeunesse Site de volley-ball plage":@"SPB",@"Parc des sports des Jeux Olympiques de la Jeunesse Terrain de hockey":@"SPH",@"Parc des sports des Jeux Olympiques de la Jeunesse Terrain de rugby":@"SPR"},
                           @"Gymnase de Longjiang":@{@"Gymnase de Longjiang":@"LJG"}};
        titleArr = [[NSArray alloc] initWithObjects:@"VOJ",
                    @"Centre d’exposition international de Nanjing",
                    @"Centre olympique de Nanjing",
                    @"Ecole des sports nautiques de Nanjing",
                    @"Site de triathlon du lac Xuanwu",
                    @"Site de Wutaishan",
                    @"Centre sportif de Jiangning",
                    @"Parc des sports des Jeux Olympiques de la Jeunesse",
                    @"Gymnase de Longjiang",
                    nil];
    }
    
    
    
    imageNameArr = @[[UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC南京青奥村" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC南京国际博览中心" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC南京奥体中心" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC南京水上运动学校" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC玄武湖铁人三项赛场" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC五台山" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC江宁体育中心" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC青奥体育公园" ofType:@"jpg"]],
                     [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HC龙江体育馆" ofType:@"jpg"]]];
    NSLog(@"over");
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int  count= titleArr.count%2==0?0:1;
    return titleArr.count/2+count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 150, 105)];
    image1.image = imageNameArr[indexPath.row*2];
    image1.userInteractionEnabled = YES;
    [cell.contentView addSubview:image1];
    
    UIImageView *back1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, image1.frame.size.height-30, image1.frame.size.width, 30)];
    back1.backgroundColor = [UIColor blackColor];
    back1.alpha = 0.7;
    [image1 addSubview:back1];
    
    UILabel *title1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, back1.frame.size.width, back1.frame.size.height)];
    title1.backgroundColor = [UIColor clearColor];
    title1.text = titleArr[indexPath.row*2];
    title1.font = HEIGHT_FONT(13.0f);
    title1.textAlignment = NSTextAlignmentCenter;
    title1.textColor = [UIColor whiteColor];
    [back1 addSubview:title1];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = image1.frame;
    [button1 addTarget:self action:@selector(facilityInfo:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = indexPath.row*2;
    [cell.contentView addSubview:button1];
    
    if(indexPath.row*2+1 < 9)
    {
        UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(165, 5, 150, 105)];
        image2.image = imageNameArr[indexPath.row*2+1];
        image2.userInteractionEnabled = YES;
        [cell.contentView addSubview:image2];
        
        UIImageView *back2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, image1.frame.size.height-30, image1.frame.size.width, 30)];
        back2.backgroundColor = [UIColor blackColor];
        back2.alpha = 0.7;
        [image2 addSubview:back2];
        
        UILabel *title2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, back1.frame.size.width, back1.frame.size.height)];
        title2.backgroundColor = [UIColor clearColor];
        title2.text = titleArr[indexPath.row*2+1];
        title2.font = HEIGHT_FONT(13.0f);
        title2.textAlignment = NSTextAlignmentCenter;
        title2.textColor = [UIColor whiteColor];
        [back2 addSubview:title2];
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.frame = image2.frame;
        [button2 addTarget:self action:@selector(facilityInfo:) forControlEvents:UIControlEventTouchUpInside];
        button2.tag = indexPath.row*2+1;
        [cell.contentView addSubview:button2];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)facilityInfo:(UIButton *)button
{
    if(tempSelectDic)
        tempSelectDic = nil;
    tempSelectDic = [faclityCodeDic objectForKey:titleArr[button.tag]];
    
    NSLog(@"tempSelectDic-->%@",tempSelectDic);
    
    if([tempSelectDic allKeys].count == 1)
        [self goto3D:tempSelectDic[titleArr[button.tag]]];
    else
    {
        if(tempSelectArr)
        {
            tempSelectArr = nil;
        }
        
        tempSelectArr = [tempSelectDic allKeys];
        //            tempSelectArr = [tempSelectArr sortedArrayUsingSelector:@selector(compare:)];
        
        if(custom)
        {
            [custom removeFromSuperview];
            custom = nil;
        }
        custom = [[HCCustomSelect alloc] initWithFrame:INDICATOR_VIEW_FRAME title:NSLocalizedString(@"HC请选择 场馆", @"") arr:tempSelectArr];
        custom.delegate = self;
        [self.view addSubview:custom];
    }
}

- (void)selectDelegate:(int)buttonTag
{
    if(custom)
        [custom removeFromSuperview];
    
    NSLog(@"-->%d",buttonTag);
    
    if(0 <= buttonTag && buttonTag < 4)
        [self goto3D:tempSelectDic[[tempSelectDic allKeys][buttonTag]]];
}

- (void)goto3D:(NSString *)codeStr
{
    self.mm_drawerController.leftDrawerViewController = nil;
    self.mm_drawerController.rightDrawerViewController = nil;
    NSLog(@"goto3D-->%@",codeStr);
    ShowVenuesViewController *ctl = [[ShowVenuesViewController alloc]init];
    NSLog(@"SceneNo-->%@",codeStr);
    ctl.SceneNo = codeStr;
    ctl.LangType = NSLocalizedString(@"HC3Dlanguage", @"");
    [self.navigationController pushViewController:ctl animated:YES];
    [delegate selectVenue:codeStr];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
